// pages/index/index.tsx
import React, { useState } from 'react'
import { View, Text } from '@tarojs/components'
import {Button, Dialog, Input} from "@nutui/nutui-react-taro";
import HeaderTitle from "@/components/HeaderTitle";
import './index.scss'
import Taro from "@tarojs/taro";
import adminService from "@/shared/services/admin/admin.service";
import AdminService from "@/shared/services/admin/admin.service";
import {loadingService} from "@/shared/utils/loading";


const Index: React.FC = () => {
  const [phone, setPhone] = useState('')
  const [remark, setRemark] = useState('')
  const [realName, setRealName] = useState('')
  const [visible, setVisible] = useState(false)

  const currentPage = Taro.getCurrentPages().pop();
  const eventChannel = currentPage?.getOpenerEventChannel();

  Taro.useLoad(async (options) => {
    console.log('页面参数:', options);

    // 直接使用 options 获取参数
    setPhone(options.phone)

    loadingService.show({})
    let result = await AdminService.singleUser({ phone: options.phone });
    loadingService.hide()
    if(result.code == 0){
        setRemark(result.data.remark)
        setRealName(result.data.realName)
    }else{

    }
  });

  const back = () => {
    if (eventChannel) {
      // 触发刷新事件
      eventChannel.emit('refreshData');
    }
    Taro.navigateBack()
  }

  const toSaveTag = async () => {
    if(!phone || !remark || !realName){
      Taro.showToast({
        title: '请输入完整信息',
        icon: 'none'
      })
      return
    }

    loadingService.show({})
      const resp = await adminService.addRemark({
        phone: phone,
        remark: remark,
        realName: realName,
      })
    loadingService.hide()
    if(resp.code == 0){
      setVisible(true)
    }else{
      Taro.showToast({
        title: resp.msg,
        icon: 'none'
      })
    }
  }

  return (
    <View className="pageContainer">
      {/* Header */}
      <View className='order-header'>
        <HeaderTitle title={'修改用户信息'} />
      </View>

      <View>

        <View className="inputGroup">
          <View className="inputItem">
            <Text className="label">手机号</Text>
            <Input
              className="customInput"
              placeholder="请输入手机号"
              type="digit"
              value={phone}
              onChange={(val) => setPhone(val)}
            />
          </View>
          <View className="inputItem">
            <Text className="label">用户真实姓名</Text>
            <Input
              className="customInput"
              placeholder="请输入用户真实姓名"
              value={realName}
              onChange={(val) => setRealName(val)}
            />
          </View>
          <View className="inputItem">
            <Text className="label">用户备注</Text>
            <Input
              className="customInput"
              placeholder="请输入用户备注"
              value={remark}
              onChange={(val) => setRemark(val)}
            />
          </View>
        </View>

        <View className='confirm'>
          <Button
            block={true}
            className='to-save'
            onClick={toSaveTag}
            type='primary'
            color='#121214'
          >确定</Button>
        </View>

      </View>

      <Dialog
        title="修改用信息成功"
        visible={visible}
        confirmText={"返回"}
        cancelText={"继续修改"}
        onConfirm={() => back()}
        onCancel={() => setVisible(false)}
      >
        <Text className="mark">修改后的姓名: {realName}</Text>
        <Text className="mark">修改后的备注: {remark}</Text>
      </Dialog>

    </View>
  )
}

export default Index
